package service

import (
	"goseckill/dao"
	"goseckill/models"
)

// 接口
type IProductService interface{
	// 定义方法
	GetProductByID(int64) (*models.Product, error)
	GetAllProduct() ([]*models.Product, error)
	DeleteProductByID(int64) bool
	InsertProduct(product *models.Product) (int64, error)
	UpdateProduct(product *models.Product) error
	SubNumberOne(productID int64) error
}
// 接口实现类
type ProductService struct{
	ProductDao dao.IProduct
}
// New
func NewProductService(dao dao.IProduct) IProductService {
	return &ProductService{
		ProductDao: dao,
	}
}

// 实现接口
func (p *ProductService) GetProductByID(pid int64) (*models.Product, error){
	return p.ProductDao.SelectByKey(pid)
}
func (p *ProductService)GetAllProduct() ([]*models.Product, error){
	return p.ProductDao.SelectAll()
}
func (p *ProductService)DeleteProductByID(pid int64) bool {
	return p.ProductDao.Delete(pid)
}
func (p *ProductService)InsertProduct(product *models.Product) (int64, error){
	return p.ProductDao.Insert(product)
}
func (p *ProductService)UpdateProduct(product *models.Product) error {
	return p.ProductDao.Update(product)
}
func (p *ProductService)SubNumberOne(productID int64) error {
	return p.ProductDao.SubProductNum(productID)
}